﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Moonlite.ExtensionApi.GlobalInterfaces;

namespace Moonlite.Platform.General {
    public static class Global {
        public const int FolderClosedImageIndex = 0;
        public const int FolderOpenImageIndex = 1;
        public const int FileGalaxyImageIndex = 2;
        public const int FileJassImageIndex = 3;
        public const int FileXmlImageIndex = 6;
        public const int FileGenericImageIndex = 7;
        public const int FileImageImageIndex = 8;
        public const int GalaxyProjectImageIndex = 4;
        public const int JassProjectImageIndex = 5;
        public const int ProjectImageIndex = 9;

        public const string Warcraft3RegistryPath = "Software\\Blizzard Entertainment\\Warcraft III";
        public const string Warcraft3RegistryKeyName = "Program";
        public const string Warcraft3XRegistryKeyName = "ProgramX";

        public const string Starcraft2RegistryPath = "SOFTWARE\\Blizzard Entertainment\\StarCraft II Retail";
        public const string Starcraft2RegistryKeyName = "GamePath";
        public const string Starcraft2XRegistryKeyName = "GamePathX";

        public const string SourceCodeFolderName = "Source";
        public const string BinFolderName = "Bin";
        public const string TempFolderName = "Temp";
        public const string ObjFolderName = "obj";
        public const string ReferenceCodeFolderName = "References";
        public const string SettingsFolderName = "Settings";
        public const string CompilersFolderPath = "Compilers";
        public const string AndromedaOutputFolderPath = "LangParser";

        public const string DefaultWc3MapPath = "Temp\\DefaultMap" + "." + WarcraftMapExtension;
        public const string DefaultSc2MapPath = "Temp\\DefaultMap" + "." + StarcraftMapExtension;

        public const string AndromedaObjectsFileName = "Objects.xml";
        public const string AndromedaErrorsFileName = "Errors.xml";
        public const string DockLayoutFileName = "DockLayout.xml";

        public const string WarcraftMapExtension = "w3x";
        public const string StarcraftMapExtension = "SC2Map";

        public const string JassProjectExtension = "jxproj";
        public const string GalaxyProjectExtension = "gxproj";
        public const string JassFileExtension = "jx";
        public const string GalaxyFileExtension = "gx";
        public const string MoonliteTemplateFileExtension = "mtp";
        public const string XimlFileExtension = "ximl";
        public const string CompiledLibraryExtension = "cml";
        public const string XmlFileExtension = "xml";
        public const string XsdFileExtension = "xsd";
        public const string BmpFileExtension = "bmp";
        public const string PngFileExtension = "png";
        public const string BlpFileExtension = "blp";
        public const string TgaFileExtension = "tga";
        public const string DdsFileExtension = "dds";
        public const string ProjectSettingsFileExtension = "projsettings";
        public static Dictionary<string, int> ProjectManagerAvailableFileExtensions = new Dictionary<string, int>();
        public static Hashtable AssociatedBuildActions = new Hashtable();

        public const string XmlDefaultHeader = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";

        public const int CompiledLibraryCommentLength = 10;
        public static readonly byte[] CompiledLibraryStartComment = new byte[] { 0x9f, 0xb5, 0x96, 0xe6, 0xc9, 0x69, 0x2b, 0x98, 0xf9, 0xdb };
        public static readonly byte[] CompiledLibraryEndFilesComment = new byte[] { 0xa6, 0xb6, 0x65, 0xad, 0x84, 0x4b, 0x4c, 0xa8, 0xfd, 0x2b  };
        public static readonly byte[] CompiledLibraryEndReferencesComment = new byte[] { 0xea, 0x94, 0xa1, 0xa5, 0x5c, 0xb0, 0x00, 0xa0, 0x1c, 0x5d  };
        public static readonly byte[] CompiledLibraryEndComment = new byte[] { 0x7f, 0xd6, 0x9f, 0xe9, 0xbf, 0x36, 0x00, 0x5c, 0x29, 0x87  };

        public static IMoonliteShell Instance { get; set; }
        public static bool IsFileValidProject(string filename) {
            string extension = Path.GetExtension(filename).Trim('.');
            switch (extension) {
                case JassProjectExtension:
                case GalaxyProjectExtension:
                    return true;
            }
            return false;
        }
        public static string GetAndromedaOutputFolderPath() {
            return Application.StartupPath + "\\" + AndromedaOutputFolderPath;
        }

        public static string GetProjectsFolder() {
            return String.Format("{0}\\Moonlite Map Studio\\Projects", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
        }

        public static string GetLibrariesFolder() {
            return String.Format("{0}\\Moonlite Map Studio\\Libraries", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
        }

        public static string GetAssemblyCompany(Assembly assembly) {
            object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
            if (attributes.Length == 0) {
                return "";
            }

            return ((AssemblyCompanyAttribute)attributes[0]).Company;
        }

        public static string GetAssemblyTitle(Assembly assembly) {
            object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
            if (attributes.Length == 0) {
                return "";
            }

            return ((AssemblyTitleAttribute)attributes[0]).Title;
        }

        public static Version GetMoonliteVersion() {
            var assembly = typeof(Global).Assembly;
            var assemblySignature = assembly.GetName();
            return assemblySignature.Version;
        }

        public static string GetAppDataFolder() {
            var assembly = typeof(Global).Assembly;
            return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\" + GetAssemblyCompany(assembly) + "\\" + GetAssemblyTitle(assembly);
        }

        public static string GetAppDataSettingsFolder() {
            return GetAppDataFolder() + "\\" + SettingsFolderName;
        }

        public static string GetTempFolderPath() {
            return Application.StartupPath + "\\Temp";
        }

        static Global() {
            ProjectManagerAvailableFileExtensions.Add(JassFileExtension, FileJassImageIndex);
            ProjectManagerAvailableFileExtensions.Add(GalaxyFileExtension, FileGalaxyImageIndex);
            ProjectManagerAvailableFileExtensions.Add(XmlFileExtension, FileXmlImageIndex);
            ProjectManagerAvailableFileExtensions.Add(BmpFileExtension, FileImageImageIndex);
            ProjectManagerAvailableFileExtensions.Add(PngFileExtension, FileImageImageIndex);
            ProjectManagerAvailableFileExtensions.Add(BlpFileExtension, FileImageImageIndex);
            ProjectManagerAvailableFileExtensions.Add(TgaFileExtension, FileImageImageIndex);
            ProjectManagerAvailableFileExtensions.Add(DdsFileExtension, FileImageImageIndex);
        }
    }
}
